package com.easyadmin.modules.job.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.easyadmin.common.quartz.base.BaseQuartz;
import com.easyadmin.modules.job.domain.ScheduleLog;
import com.easyadmin.modules.job.mapper.ScheduleLogMapper;
import com.easyadmin.modules.stock.domain.*;
import com.easyadmin.modules.stock.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;


/**
 *  异步多线程实现入 预警表
 *  异步多线程执行任务  比较快
 *
 *  1.股票价格预警，入预警库
 *
 *
 *
 *
 *
 *   预警等相关数据的处理
 *
 *
 */

@Slf4j
@Component("priceWarnDate")
public class PriceWarnDate implements BaseQuartz {

    @Autowired
    ThreadPoolExecutor executor;
    @Autowired
    SysStockPriceWarnDao sysStockPriceWarnDao;
    @Autowired
    private ScheduleLogMapper scheduleLogMapper;
    @Autowired
    SysTradeDateDao sysTradeDateDao;
    @Autowired
    private SysFuturesDayPriceDao sysFuturesDayPriceDao;
    @Autowired
    SysExpectPlanDao sysExpectPlanDao;
    @Autowired
    SysStockAgainWarnDao sysStockAgainWarnDao;
    @Autowired
    SysSeriousWarnDao sysSeriousWarnDao;


    @Override
    public void run(String params) throws Exception {
        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 转换为字符串
        String todayStr = today.format(formatter);
        //判断当日数据是否执行
        List<ScheduleLog> scheduledata= scheduleLogMapper.selectCreateTime(todayStr,"priceWarnDate");
        if(scheduledata.size()>0){
            return;
        }

        List<SysTradeDateEntity> DateLists =sysTradeDateDao.selectList(new QueryWrapper<SysTradeDateEntity>()
                .lambda().eq(SysTradeDateEntity::getSysDate,todayStr));
        if(DateLists.size()<1){
            //非交易日不执行
            return;
        }

        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            //股票数据入预警库数据
            sysStockPriceWarnDao.insertStockPriceWarn(todayStr);
        }, executor);
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            //行业数据入预警库数据
            sysStockPriceWarnDao.insertIndustryPriceWarn(todayStr);
        }, executor);
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            //ETF数据入预警库数据
            sysStockPriceWarnDao.insertEtfPriceWarn(todayStr);
        }, executor);

        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1,future2,future3);

        //处理预警想的流程价格

        //查询流程没有完成的数据
        List<SysExpectPlan> planLists =sysExpectPlanDao.selectList(new QueryWrapper<SysExpectPlan>().lambda()
                .eq(SysExpectPlan::getWorkerFlag,"0")
                .isNotNull(SysExpectPlan::getExpectPrice));
        //如果当前价格小于预期价格,并且不存在 则插入二次预警价格，如果存在，则不进行任何操作,进二次预警
        if(planLists.size()>0){
            planLists.forEach(plan -> {
                // 处理每个plan对象
                SysStockAgainWarn warn=sysStockAgainWarnDao.selectOne(new QueryWrapper<SysStockAgainWarn>().lambda()
                        .eq(SysStockAgainWarn::getPlanId,plan.getId()));
                SysStockPriceWarnEntity entity= sysStockPriceWarnDao.selectOne(new QueryWrapper<SysStockPriceWarnEntity>()
                        .lambda().eq(SysStockPriceWarnEntity::getId,plan.getWarnId()));
                if (ObjectUtils.isNull(warn) || ObjectUtils.isEmpty(warn)) {
                    if(Integer.valueOf(plan.getExpectPrice()) <Integer.valueOf(entity.getPrice())){
                        SysStockAgainWarn againWarn=new SysStockAgainWarn();
                        againWarn.setProId(plan.getProId());
                        againWarn.setProName(plan.getProName());
                        againWarn.setPlanId(plan.getId());
                        againWarn.setMarket(plan.getMarket());
                        againWarn.setWarnTime(new Date());
                        againWarn.setAuditStatus("0");
                        sysStockAgainWarnDao.insert(againWarn);
                    }
                }
            });
        }
        //如何进入严重告警，低于二次预警的价格
        List<SysStockAgainWarn> warnlists=sysStockAgainWarnDao.selectList(new QueryWrapper<SysStockAgainWarn>().lambda()
                .eq(SysStockAgainWarn::getAuditStatus,"2")
                .isNotNull(SysStockAgainWarn::getWarnPrice));
        if(warnlists.size()>0){


        }






        log.info("预警任务执行完成");
    }
}
